/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  MainJYXFragment.java   
 * @Package com.jiyi.activity.main   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-6-22 下午3:03:35   
 * @version V1.0     
 */
package com.jiyi.activity.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.jiyi.activity.zone.PersonInfoActivity;
import com.jiyi.activity.zone.ZoneAllHuoDongActivity;
import com.jiyi.adapter.CommonAdapter;
import com.jiyi.adapter.ViewHolder;
import com.jiyi.bean.main.JiYouXiuJson;
import com.jiyi.bean.main.MainJiYouXiuBean;
import com.jiyi.bean.main.NewsListJson.NewsBean;
import com.jiyi.bean.main.TopicCatBean;
import com.jiyi.bean.zone.HuoDongDetailJson;
import com.jiyi.bean.zone.ZoneHuoDongBean;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.inteface.HuoDongPopInterface;
import com.jiyi.inteface.WebApi;
import com.jiyi.view.HuoDongPopView;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;
import com.swift.maintenance.utils.ZoneHuoDongUtil;
import com.swift.utils.DateUtils;
import com.swift.utils.GlideUtil;
import com.swift.widget.TranslucentImageView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainJYXFragment extends Fragment implements HuoDongPopInterface {

	@BindView(R.id.llHuaTi)
	LinearLayout llHuaTi;

	@BindView(R.id.gvJYX)
	GridView gvJYX;

	@BindView(R.id.ivHotHuaTiMore)
	ImageView ivHotHuaTiMore;
	@BindView(R.id.tvJiYouXiuMore)
	TextView tvJiYouXiuMore;
	@BindView(R.id.ivNewsLogo)
	ImageView ivNewsLogo;
	@BindView(R.id.tvNewsTitle)
	TextView tvNewsTitle;
	@BindView(R.id.tvReadNum)
	TextView tvReadNum;
	@BindView(R.id.ivZhiShiLogo)
	ImageView ivZhiShiLogo;
	@BindView(R.id.tvZhiShiTitle)
	TextView tvZhiShiTitle;
	@BindView(R.id.tvZhiShiReadNum)
	TextView tvZhiShiReadNum;
	@BindView(R.id.tvZhiShiContent)
	TextView tvZhiShiContent;
	
	
	@BindView(R.id.ivHuoDongLogo)
	TranslucentImageView ivHuoDongLogo;
	@BindView(R.id.tvStartDate)
	TextView tvStartDate;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvEndDate)
	TextView tvEndDate;
	@BindView(R.id.tvAddress)
	TextView tvAddress;
	@BindView(R.id.tvJoin)
	TextView tvJoin;
	
	

	List<TopicCatBean> huaTiList = new ArrayList<TopicCatBean>();
	List<MainJiYouXiuBean> coverList = new ArrayList<MainJiYouXiuBean>();
	private CommonAdapter mAdapter;
	
	HuoDongPopView mHuoDongPopView;

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_main_jyx, null, false);
		ButterKnife.bind(this, view);
		init();
		return view;
	}

	/**
	 * @author: HYM
	 * @Title: init
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-6-23
	 */
	private void init() {
//		initHuaTiData();
		setJYXAdapter();
		getData();
		
		mHuoDongPopView = new HuoDongPopView(getContext(), this);
	}

	/**   
	 * @author: HYM 
	 * @Title: getData   
	 * @Description: TODO(这里用一句话描述这个方法的作用)    void 
	 * @date: 2017-7-22
	 */  
	private void getData() {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());

		VolleyRequest.StringRequestPost(getContext(), WebApi.MAIN_JIYOUXIU_CONTENT, "",
				params, new StringInterface(getContext()) {

					@Override
					public void stringSuccess(String result) {
						if (getContext() == null) {
							return;
						}
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								JiYouXiuJson temp = new Gson().fromJson(result, JiYouXiuJson.class);
								huaTiList.clear();
								huaTiList.addAll(temp.getData().getCatList());
								fillHuaTiData();
								coverList.clear();
								coverList.addAll(temp.getData().getFriendShowList());
								mAdapter.notifyDataSetChanged();
								final NewsBean zixun = temp.getData().getHotArt();
								GlideUtil.load(getContext(), zixun.getImage(), ivNewsLogo);
								tvReadNum.setText(zixun.getReading()+"");
								tvNewsTitle.setText(zixun.getTitle());
								final NewsBean zhishi = temp.getData().getKnowledge();
								GlideUtil.load(getContext(), zhishi.getImage(), ivZhiShiLogo);
								tvZhiShiReadNum.setText(zhishi.getReading()+"");
								tvZhiShiTitle.setText(zhishi.getTitle());
								
								tvZhiShiTitle.setOnClickListener(new OnClickListener() {
									
									@Override
									public void onClick(View v) {
										tvZhiShiReadNum.setText((Integer.parseInt(tvZhiShiReadNum.getText().toString())+1)+"");
										Intent intent = new Intent(v.getContext(),ZhiShiDetailActivity.class);
										intent.putExtra("id", zhishi.getId());
										intent.putExtra("type", "zhishi");
										startActivity(intent);
									}
								});
								ivZhiShiLogo.setOnClickListener(new OnClickListener() {
									
									@Override
									public void onClick(View v) {
										tvZhiShiReadNum.setText((Integer.parseInt(tvZhiShiReadNum.getText().toString())+1)+"");
										Intent intent = new Intent(v.getContext(),ZhiShiDetailActivity.class);
										intent.putExtra("id", zhishi.getId());
										intent.putExtra("type", "zhishi");
										startActivity(intent);
									}
								});
								
								tvZhiShiContent.setText(zhishi.getContent());
								
								
								tvNewsTitle.setOnClickListener(new OnClickListener() {
									
									@Override
									public void onClick(View v) {
										tvReadNum.setText((Integer.parseInt(tvReadNum.getText().toString())+1)+"");
										Intent intent = new Intent(v.getContext(),ZhiShiDetailActivity.class);
										intent.putExtra("id", zixun.getId());
										intent.putExtra("type", "zixun");
										startActivity(intent);
									}
								});
								ivNewsLogo.setOnClickListener(new OnClickListener() {
									
									@Override
									public void onClick(View v) {
										tvReadNum.setText((Integer.parseInt(tvReadNum.getText().toString())+1)+"");
										Intent intent = new Intent(v.getContext(),ZhiShiDetailActivity.class);
										intent.putExtra("id", zixun.getId());
										intent.putExtra("type", "zixun");
										startActivity(intent);
									}
								});
								
								final ZoneHuoDongBean huoDongBean = temp.getData().getActivity();
								GlideUtil.load(getContext(), huoDongBean.getCover(), ivHuoDongLogo);
								ivHuoDongLogo.setTextString(huoDongBean.getAttendPer() + "人参与");
								tvTitle.setText(huoDongBean.getTheme());
								tvStartDate.setText(DateUtils.fmtDateToStr(huoDongBean.getStart_time(), DateUtils.YMD_FORMAT));
								tvEndDate.setText(DateUtils.fmtDateToStr(huoDongBean.getEnd_time(), DateUtils.YMD_FORMAT));
								tvAddress.setText(huoDongBean.getAddress());
								ZoneHuoDongUtil.setStatus(tvJoin, huoDongBean.getFlag());
								tvJoin.setOnClickListener(new OnClickListener() {
									
									@Override
									public void onClick(View v) {
										getHuoDongDetail(huoDongBean.getId());
									}
								});
								
								
								
							} else {
								ToastUtil.getInstances().showMsg("服务器连接异常");
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
						}
					}

					@Override
					public void stringError(VolleyError error) {
						ToastUtil.getInstances().showMsg("网络连接失败,请检查您的网络");
					}
				});
	}


	void fillHuaTiData() {
		llHuaTi.removeAllViews();
		for (int i = 0; i < huaTiList.size(); i++) {
			final TopicCatBean obj = huaTiList.get(i);
			View view = LayoutInflater.from(getContext()).inflate(
					R.layout.item_main_jyx_huati, null, false);
			//动态加上内容权重
			LayoutParams params =new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f);
			view.setLayoutParams(params);  
			TextView tvHuaTiName = (TextView) view
					.findViewById(R.id.tvHuaTiName);
			tvHuaTiName.setText(obj.getName());
			ImageView ivIcon = (ImageView) view.findViewById(R.id.ivIcon);
			GlideUtil.load(getContext(), obj.getIndex_image(), ivIcon);
			LinearLayout llItem = (LinearLayout) view.findViewById(R.id.llItem);
			llItem.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(getActivity(),
							MainHuaTiDetailActivity.class);
					intent.putExtra("topicCatBean", new Gson().toJson(obj));
					startActivity(intent);
				}
			});
			llHuaTi.addView(view);
		}
	}

	/**
	 * @author: HYM
	 * @Title: setAdapter
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-6-23
	 */
	private void setJYXAdapter() {
		mAdapter = new CommonAdapter<MainJiYouXiuBean>(getActivity(), coverList,
				R.layout.item_image) {

			@Override
			public void convert(ViewHolder holder, MainJiYouXiuBean t) {
				holder.setImage(R.id.ivImage, t.getImage());
			}
		};
		gvJYX.setAdapter(mAdapter);
		gvJYX.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				MainJiYouXiuBean jyx = (MainJiYouXiuBean) parent.getAdapter().getItem(position);
				Intent intent = new Intent(getActivity(),PersonInfoActivity.class);
				intent.putExtra("id", jyx.getUser_id());
				startActivity(intent);
			}
		});
	}

	@OnClick({ R.id.ivHotHuaTiMore, R.id.ivZiXunMore, R.id.ivZhiShiMore,
			R.id.ivJiYouXiuMore, R.id.ivGYHuoDongMore, R.id.tvJiYouXiuMore,
			R.id.ivZhiShiLogo, R.id.tvZhiShiTitle, R.id.ivNewsLogo,
			R.id.tvNewsTitle })
	public void onClick(View view) {
		MainFragment mainFragment;
		Intent intent;
		switch (view.getId()) {
		case R.id.ivHotHuaTiMore:
			mainFragment = (MainFragment) getParentFragment();
			mainFragment.setCurrentItem(1);
			break;
		case R.id.ivZiXunMore:
			mainFragment = (MainFragment) getParentFragment();
			mainFragment.setCurrentItem(3);
			break;
		case R.id.ivZhiShiMore:
			mainFragment = (MainFragment) getParentFragment();
			mainFragment.setCurrentItem(2);
			break;
		case R.id.tvJiYouXiuMore:
		case R.id.ivJiYouXiuMore:
			intent = new Intent(getActivity(), JiYouXiuListActivity.class);
			startActivity(intent);
			break;
		case R.id.ivGYHuoDongMore:
			intent = new Intent(getActivity(), ZoneAllHuoDongActivity.class);
			startActivity(intent);
			break;
		
		
		default:
			break;
		}
	}

	/**   
	 * <p>Title: OnSureListener</p>   
	 * <p>Description: </p>      
	 * @see HuoDongPopInterface#OnSureListener()
	 */ 
	@Override
	public void OnSureListener() {
		// TODO Auto-generated method stub
		
	}

	/**   
	 * <p>Title: OnCancleListener</p>   
	 * <p>Description: </p>      
	 * @see HuoDongPopInterface#OnCancleListener()
	 */ 
	@Override
	public void OnCancleListener() {
		// TODO Auto-generated method stub
		
	}

	/**   
	 * <p>Title: OnShowListener</p>   
	 * <p>Description: </p>      
	 * @see HuoDongPopInterface#OnShowListener()
	 */ 
	@Override
	public void OnShowListener() {
		// TODO Auto-generated method stub
		
	}

	/**   
	 * <p>Title: OnHideListener</p>   
	 * <p>Description: </p>      
	 * @see HuoDongPopInterface#OnHideListener()
	 */ 
	@Override
	public void OnHideListener() {
		// TODO Auto-generated method stub
		
	}
	
	void getHuoDongDetail(String huoDongId) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());
		params.put("id", huoDongId);

		VolleyRequest.StringRequestPost(getContext(),
				WebApi.ZONE_HUODONG_DETAIL, "", params, new StringInterface(
						getContext()) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								HuoDongDetailJson temp = new Gson().fromJson(
										result, HuoDongDetailJson.class);
								mHuoDongPopView.showPopWindow(temp.getData());
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
						}
					}

					@Override
					public void stringError(VolleyError error) {
						ToastUtil.getInstances().showMsg(R.string.err_http_response);
					}
				});
	}
}
